package tedu.boot321.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tedu.boot321.entity.User;
import tedu.boot321.mapper.UserMapper;

@RestController
public class UserController {

    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            return 2;//代表用户已存在
        }
        //执行注册
        mapper.insertUser(user);
        return 1;//代表注册成功
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            if(user.getPassword().equals(u.getPassword())){
                return 1;//登录成功!
            }
            return 3;//密码错误!
        }
        return 2;//代表用户名不存在
    }
}
